﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

using EDTS.Model.Users;
using EDTS.Data.Users;

namespace EDTS.Logic.Users
{
    public class UserLogic
    {
        public static UserModel GetModel(Guid guid)
        {
            return UserCommand.GetModel(guid);
        }
        public static UserModel GetModel(string userName)
        {
            return UserCommand.GetModel(userName);
        }

        public static void CreateNew(UserModel model)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserCommand.CreateNew(model);

                var token = EDTS.Logic.Weixin.WxAppLogic.GetAccessToken(model.Unit.WxAppID.Value);
                EDTS.Weixin.Submits.User.Create(token, model.UserName, model.RealName, model.Unit.WeixinDepartmentID.Value, model.Mobile, model.Email, model.WeixinID);

                ts.Complete();
            }
        }

        public static void Update(UserModel model)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                UserCommand.Update(model);

                var token = EDTS.Logic.Weixin.WxAppLogic.GetAccessToken(model.Unit.WxAppID.Value);
                EDTS.Weixin.Submits.User.Update(token, model.UserName, model.RealName, model.Unit.WeixinDepartmentID.Value, model.Mobile, model.Email, model.WeixinID);

                ts.Complete();
            }
        }

        public static void Interest(string userName, bool it)
        {
            UserCommand.Interest(userName, it);
        }
    }
}
